package com.herench.bunch.cmis.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.group.RTB_K13_ROW_DEFINITION;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.message.RTB_K13;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.QRD;
import ca.uhn.hl7v2.model.v24.segment.RDT;
import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author luoxiaoming on 18-10-16 上午10:30
 * @email luoxiaoming@herench.com
 */
public class DeptDictConvert extends AbstractMessageConvert {

    @Override
    public Map<String,Object> toMessage(RequestMessage requestMessage) throws Exception {
        return null;
    }

    @Override
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>> ) message;
        RTB_K13 rtbK13 = new RTB_K13();
        rtbK13.initQuickstart("RTB_K13", "RTB_K13", "P");
        MSH msh = rtbK13.getMSH();
        initResponseMessageMSH(msh);

        RTB_K13_ROW_DEFINITION definition = rtbK13.getROW_DEFINITION();
        int index = 0;
        for (Map<String,Object> resMap : res) {
            RDT rdt = new RDT(rtbK13, new DefaultModelClassFactory());
            CE ce = new CE(rtbK13);
            ce.getCe1_Identifier().setValue("RD");
            StringBuffer buffer = new StringBuffer();
            buffer.append("dbUser=").append(resMap.get("dbUser")).append("#")
                    .append("userId=").append(resMap.get("userId")).append("#")
                    .append("userName=").append(resMap.get("userName")).append("#")
                    .append("userDept=").append(resMap.get("userDept")).append("#")
                    .append("password=").append(resMap.get("password"));
            ce.getCe2_Text().setValue(buffer.toString());
            rdt.getColumnValue().setData(ce);
            definition.insertRDT(rdt, index++);
        }
        return rtbK13;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_Q01 qry = new QRY_Q01();
        qry.initQuickstart("QRY_Q01", "Q01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("300030");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("LIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        QRD qrd = qry.getQRD();
        addQrd(qrd, "testNo", "Z100610000001A");
        addQrd(qrd, "resultStatus", "2");
        addQrd(qrd, "reporter", "1503");
        addQrd(qrd, "verifiedBy", "1503");
        return qry;
    }
}
